package com.ushio.capsuleservice.controller;

import com.ushio.capsuleservice.model.dto.ApiResponse;
import com.ushio.capsuleservice.model.entity.Archive;
import com.ushio.capsuleservice.model.entity.ArchiveNote;
import com.ushio.capsuleservice.model.entity.Container;
import com.ushio.capsuleservice.service.ArchiveService;
import com.ushio.capsuleservice.service.impl.StorageService;
import com.ushio.capsuleservice.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 适配前端的通用接口
 */
@Slf4j
@RestController
@RequestMapping("/api/common")
@CrossOrigin(origins = "*")
public class CommonController {

    @Autowired
    private StorageService storageService;
    
    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 根据关键字查询容器数据
     * @param key 查询关键字
     * @return 容器数据列表
     */
    @GetMapping("/search/{key}")
    public ApiResponse<Map<String, Object>> searchContainers(@PathVariable String key) {
        log.info("查询容器数据: {}", key);
        try {
            Map<String, Object> containers = storageService.getData(key,1L);
            return ApiResponse.success(containers);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    @PostMapping("/save")
    public ResponseEntity<String> saveData(@RequestBody SaveRequest request) {
        try {
            // 1. 解析请求体
            String key = request.getKey();
            String data = request.getData();

            // 2. 处理数据（例如存储到数据库或其他逻辑）
            // 这里可以添加您的业务逻辑
            storageService.saveData(key, data);
            // 3. 返回成功响应
            return ResponseEntity.ok("数据保存成功");
        } catch (Exception e) {
            // 4. 返回错误响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("数据保存失败: " + e.getMessage());
        }
    }

    // 请求体类
    public static class SaveRequest {
        private String key;
        private String data;

        // Getter 和 Setter
        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }
}
